package com.LeeCode;

/**
 * 统计「优美子数组」
 */

public class Code1248 {
    public static void main(String[] args) {
        int[] nums = {1, 1, 1, 1, 1};
        int k = 1;
        System.out.println(new Code1248().numberOfSubarrays(nums, k));
    }

    public int numberOfSubarrays(int[] nums, int k) {
        int ans = 0;
        int left1 = 0, left2 = 0, sum1 = 0, sum2 = 0;
        for (int right = 0; right < nums.length; right++) {
            if (nums[right] % 2 == 1) {
                sum1++;
                sum2++;
            }
            while (left1 <= right && sum1 >= k) {
                if (nums[left1] % 2 == 1) {
                    sum1--;
                }
                left1++;
            }
            while (left2 <= right && sum2 > k) {
                if (nums[left2] % 2 == 1) {
                    sum2--;
                }
                left2++;
            }

            ans += left1 - left2;
        }
        return ans;
    }
}
